
using System;
using System.Collections;

namespace IGlobalist.Cms.Model
{
	#region CmsClass

    [Serializable]
	public class CmsClass :ICmsClass
		{
		#region Member Variables
		
		private long _id;
		private string _name;
		private string _displayName;
		private string _description;
        private short _ClassTypeID;
       

      
		#endregion

		#region Constructors

		public CmsClass() { }

		public CmsClass( string name, string displayName, string description, string schema, short classTypeID)
		{
			this._name = name;
			this._displayName = displayName;
			this._description = description;
			this.PropertyList.FromXml(schema);
            this._ClassTypeID = classTypeID;
		}

		#endregion

		#region Public Properties

		public long ID
		{
			get {return _id;}
			set {_id = value;}
		}

		public string Name
		{
			get { return _name; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
				_name = value;
			}
		}

        public UsageType UsageType
        {
            get { return (UsageType)Enum.Parse(typeof(UsageType), PropertyList.GetPropertyValue("UsageType").ToString()); }
            set { PropertyList.SetPropertyValue("UsageType",value); }
        }

		public string DisplayName
		{
			get { return _displayName; }
			set
			{
				if ( value != null && value.Length > 100)
					throw new ArgumentOutOfRangeException("Invalid value for DisplayName", value, value.ToString());
				_displayName = value;
			}
		}

		public string Description
		{
			get { return _description; }
			set
			{
				if ( value != null && value.Length > 300)
					throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
				_description = value;
			}
		}

		public string ClassSchema
		{
            get { return this.PropertyList.ToXmlString(); }
			set
			{
				if ( value != null && value.Length > 3000)
					throw new ArgumentOutOfRangeException("Invalid value for Schema", value, value.ToString());
                if (!this.PropertyList.FromXml(value)) throw new Exception("Schema is not valid");				
                
			}
		}

        public short ClassTypeID
        {
            get { return _ClassTypeID; }
            set { _ClassTypeID = value; }
        }

 
     


        private PropertyList _PropertyList = new PropertyList();

        public PropertyList PropertyList
        {
            get { return _PropertyList; }
            set { _PropertyList = value; }
        }
        #endregion



        public void AddProperty(BaseProperty property)
        {
            _PropertyList.Add(property);
        }
        public bool RemoveProperty(string propertyName)
        {
            return _PropertyList.Remove(propertyName);
        }
        public string ToXmlString()
        {
            return _PropertyList.ToXmlString();
        }
        public bool FromXml(string xml)
        {
            return _PropertyList.FromXml(xml);
        }
		
	}

	#endregion
}
